package com.gonfre.quientelo;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.View;
import android.widget.Toast;

import com.heinrichreimersoftware.materialdrawer.DrawerFrameLayout;
import com.heinrichreimersoftware.materialdrawer.structure.DrawerHeaderItem;
import com.heinrichreimersoftware.materialdrawer.structure.DrawerItem;
import com.heinrichreimersoftware.materialdrawer.structure.DrawerProfile;

import java.util.ArrayList;
import java.util.List;

import util.Tools;

/**
 * Clase para utilizar el menú de la izquierda.
 *
 * @author xionjames
 * @version 0.1
 */
public abstract class MyMenuActivity extends MyActivity {
    protected DrawerFrameLayout leftMenu = null;

    private Object[][] leftMenuItemsPurchaser = new Object[][] {
            { "Solicitar cotización", R.drawable.ic_send_grey600_24dp, RequestPriceActivity.class },
            { "Mis solicitudes", R.drawable.ic_list_grey600_24dp, null },
            { "Cotizaciones recibidas", R.drawable.ic_done_grey600_24dp, null },
            { "--" },
            { "Compras acordadas", R.drawable.ic_handshake_grey600_24dp, null },
            { "Historial de compras", R.drawable.ic_history_grey600_24dp, null },
            { "--" },
            { "Ajustes de la app", R.drawable.ic_settings_grey600_24dp, null, "Popup" },
            { "Ayuda y sugerencias", R.drawable.ic_help_grey600_24dp, null, "Popup" }
    };

    private Object[][] leftMenuItemsSeller = new Object[][] {
            { "Solicitudes recibidas", R.drawable.ic_list_grey600_24dp, RequestListActivity.class },
            { "--" },
            { "Ventas acordadas", R.drawable.ic_handshake_grey600_24dp, null },
            { "Historial de ventas", R.drawable.ic_history_grey600_24dp, null },
            { "--" },
            { "Ajustes de la app", R.drawable.ic_settings_grey600_24dp, null, "Popup" },
            { "Ayuda y sugerencias", R.drawable.ic_help_grey600_24dp, null, "Popup" }
    };

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        this.hasMenu = true;
        super.onPostCreate(savedInstanceState);

        toolbar.setNavigationIcon(R.drawable.ic_menu_white_24dp);
        //createLeftMenu();
        initLeftMenu();
    }

    protected void initLeftMenu() {
        leftMenu = (DrawerFrameLayout) findViewById(R.id.leftMenu);
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, leftMenu, this.toolbar, 0, 0) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }
        };
        leftMenu.setDrawerListener(drawerToggle);


        //Profile
        leftMenu.setProfile(
                new DrawerProfile()
                        .setAvatar(this, Tools.circleBitmap(getResources().getDrawable(R.drawable.no_foto)))
                        .setBackground(getResources().getDrawable(R.drawable.bg_menu))
                        .setName("Xionjames")
                        .setDescription("alejandro.alvarado@outlook.com")
                        .setOnProfileClickListener(new DrawerProfile.OnProfileClickListener() {
                            @Override
                            public void onClick(DrawerProfile drawerProfile) {
                                Toast.makeText(MyMenuActivity.this, "Click en perfil (Xionjames)", Toast.LENGTH_SHORT).show();

                            }
                        })
        );


        //Items
        leftMenu.addItems(this.getLeftMenuItems(leftMenu));
    }

    protected List<DrawerItem> getLeftMenuItems(final DrawerFrameLayout layout) {
        List<DrawerItem> items = new ArrayList<>();

        // es vendedor o comprador?
        Object[][] source_list;
        if (false) {
            source_list = leftMenuItemsPurchaser;
        } else {
            source_list = leftMenuItemsSeller;
        }

        // se crean los DrawerItem's
        for (final Object[] item : source_list) {
            DrawerItem drawerItem = new DrawerItem();
            if (item[0].equals("--")) { //divider
                drawerItem = new DrawerHeaderItem();
            } else {
                drawerItem.setImage(getResources().getDrawable((Integer) item[1]), DrawerItem.ICON)
                        .setTextPrimary((String) item[0])
                        .setTextMode(DrawerItem.SINGLE_LINE);

                boolean isThisActivity = false;
                if (item[2] != null) {
                    if ( this.getClass().equals((Class) item[2])) {
                        isThisActivity = true;
                    }
                }

                DrawerItem.OnItemClickListener onItemClickListener = null;
                if (!isThisActivity) {
                    onItemClickListener = new DrawerItem.OnItemClickListener() {
                        @Override
                        public void onClick(DrawerItem drawerItem, int id, int position) {
                            if (item[2] != null) {
                                Intent intent = new Intent(MyMenuActivity.this, (Class) item[2]);

                                if (params != null) {
                                    intent.putExtras(params);
                                }
                                startActivity(intent);
                                if (item.length > 3) {
                                    layout.closeDrawer();
                                } else { // No es popup! hay que matar la ventana actual
                                    finish();
                                }
                            }
                        }
                    };
                } else {
                    onItemClickListener = new DrawerItem.OnItemClickListener() {
                        @Override
                        public void onClick(DrawerItem drawerItem, int id, int position) {
                            layout.closeDrawer();
                        }
                    };
                }

                drawerItem.setOnItemClickListener(onItemClickListener);
            }

            items.add(drawerItem);
        }

        return items;
    }
}
